﻿using System;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using System.Data.Common;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Configuration;
using System.Xml;
using System.Xml.Serialization;
using SubSonic;
using SubSonic.Utilities;

namespace LMSData
{
    public partial class AssessmentController
    {
        public int GetAssessmentID(int CourseID, int Index)
        {
            AssessmentCollection coll = new AssessmentCollection();
            Query qry = new Query(Assessment.Schema).WHERE(Assessment.Columns.CourseID, CourseID);
            coll.LoadAndCloseReader(qry.ExecuteReader());
            if (coll.Count >= Index)
                return coll[Index - 1].AssessmentID;
            return -1;
        }

        public int GetAssessmentID(string CourseID, int Index)
        {
            AssessmentCollection coll = new AssessmentCollection();
            Query qry = new Query(Assessment.Schema).WHERE(Assessment.Columns.CourseID, CourseID);
            coll.LoadAndCloseReader(qry.ExecuteReader());
            if (coll.Count >= Index)
                return coll[Index - 1].AssessmentID;
            return -1;
        }

        public AssessmentCollection GetAssessments(string CourseID)
        {
            AssessmentCollection coll = new AssessmentCollection();
            Query qry = new Query(Assessment.Schema).WHERE(Assessment.Columns.CourseID, CourseID);
            coll.LoadAndCloseReader(qry.ExecuteReader());
            return coll;
        }

        public bool AssessmentItemsProcessed(int AssessmentID)
        {
            AssessmentItemCollection coll = new AssessmentItemCollection();
            Query qry = new Query(AssessmentItem.Schema).WHERE(AssessmentItem.Columns.AssessmentID, AssessmentID);
            coll.LoadAndCloseReader(qry.ExecuteReader());
            if (coll.Count > 0)
                return true;
            return false;
        }

        public string CreateRecord(string CourseID, int Index, string AssessmentName)
        {
            int cid = -1;
            int aid = -1;
            Int32.TryParse(CourseID, out cid);
            aid = GetAssessmentID(CourseID, Index);
            if (aid > -1)
                return aid.ToString();

            Assessment asmt = new Assessment();
            asmt.AssessmentName = AssessmentName;
            asmt.CourseID = cid;
            asmt.Save();

            return asmt.AssessmentID.ToString();
        }
    }
}
